package com.mooc.yohoshop.ui.base;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.mooc.yohoshop.entity.BaseEntity;
import com.mooc.yohoshop.entity.ErroEntity;
import com.mooc.yohoshop.network.BaseRepository;
import com.mooc.yohoshop.widget.LoadingDialog;

import java.lang.reflect.ParameterizedType;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public abstract class BaseActivity<M extends BaseRepository> extends AppCompatActivity implements Observer<BaseEntity> {

    protected CompositeDisposable compositeDisposable = new CompositeDisposable();
    protected LoadingDialog dialog;
    protected M repo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(this,layout(),null);
        setContentView(view);
        BarUtils.addMarginTopEqualStatusBarHeight(view);
        BarUtils.setStatusBarLightMode(this,false);
        BarUtils.setStatusBarColor(this, Color.BLACK);
        initRepo();
        init();
        initData();
    }

    private void initRepo(){
        //反射获取泛型类型创建对象
        ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
        Class clazz = (Class) parameterizedType.getActualTypeArguments()[0];
        try {
            repo = (M) clazz.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    protected abstract int layout();

    protected abstract void init();

    protected void request(Observable observable){
        if (NetworkUtils.isConnected()) {
            observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }else {
            ErroEntity erroEntity = new ErroEntity();
            erroEntity.status = "100";
            erroEntity.message = "网络出错";
            Observable.just(erroEntity).subscribe(this);;
        }
    }

    protected abstract void initData();

    protected <T extends View>T f(int id){
        return findViewById(id);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        compositeDisposable.dispose();
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {
        compositeDisposable.add(d);
        dialog = null;
        dialog = new LoadingDialog(this);
        dialog.show();
    }

    @Override
    public void onNext(@NonNull BaseEntity baseEntity) {
        if (baseEntity.status.equals("200")) {
            result(baseEntity);
            return;
        }
    }

    @Override
    public void onError(@NonNull Throwable e) {
        ToastUtils.showShort("请求出错");
    }

    @Override
    public void onComplete() {
        dialog.dismiss();
    }

    protected abstract void result(BaseEntity baseEntity);

}
